﻿/* Author : Geeta (A0091320W)*/
/* Dated: 27 January 2012, SA-34*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObjects;
using BAL.Controller;
namespace PresentationLayer.View
{
    public partial class EmpModifyReq : System.Web.UI.Page
    {
        ModifyReqControl control = new ModifyReqControl();
        static int i = 0;
        Employee emp;
        protected void Page_Load(object sender, EventArgs e)
        {
            emp = (Employee)Session["CurrentUser"];
            //emp.EmpId = "10030";
            List<RequisitionItems> list;
            if (!(IsPostBack))
            {
                //list = control.getRequisitionemp("10001");
                //list = control.getRequisitionemp("10001");


                //list=control.getRequisitionItems();
                //String ReqFormId=control.
                
                Requisition r = control.getRequisitionId(emp.EmpId);
                if (r != null)
                {
                    int reqFormId = r.ReqFormId;
                    i = r.ReqFormId;
                    lblReqFormId.Text = " : "+reqFormId.ToString();
                    list = control.getRequisitionItems(reqFormId);

                    if (list != null)
                    {
                        ReqDetailsView.DataSource = list;
                        ReqDetailsView.DataBind();
                        Session["RequisitionItems"] = list;
                    }
                }
                else
                {
                    Label5.Text = "<font color=red>There is no applied requistion</font>";
                    lblReqFormId.Visible = false;
                }
            }
        }

        protected void ReqDetailsView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ReqDetailsView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = ReqDetailsView.Rows[e.RowIndex];
            Control region1 = row.Cells[4].FindControl("tbQty");
            TextBox tb = (TextBox)region1;
            int qty = Convert.ToInt32(tb.Text);
            Control region2 = row.Cells[4].FindControl("hdnItemVAlue");
            HiddenField hf = (HiddenField)region2;


            List<RequisitionItems> list = (List<RequisitionItems>)Session["RequisitionItems"];
            foreach (RequisitionItems rd in list)
            {
                if (rd.ItemId == hf.Value)
                {
                    rd.RequestedQty = qty;
                    //ReqDetailsView.DataSource = list;
                    //ReqDetailsView.DataBind();
                }
            }
            ReqDetailsView.DataSource = list;
            ReqDetailsView.DataBind();

            region1 = row.Cells[4].FindControl("tbQty");
            tb = (TextBox)region1;
            //Label6.Text = tb.Text;
            Session["RequisitionItems"] = list;

            //Requisition reqSession = (Requisition)Session["Requisition"];
            //RequisitionItems list = (RequisitionItems)Session["RequisitionItems"];
            //ModifyReqControl c = new ModifyReqControl();
            control.updateReqDetails(qty, i, hf.Value);



        }

        protected void ReqDetailsView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = ReqDetailsView.Rows[e.RowIndex];
            Control region2 = row.Cells[4].FindControl("hdnItemVAlue");
            HiddenField hf = (HiddenField)region2;

            //List<RequisitionItems> list = control.getRequisitionItems(1);
            List<RequisitionItems> list = (List<RequisitionItems>)Session["RequisitionItems"];

            RequisitionItems delRD = new RequisitionItems();
            foreach (RequisitionItems rd in list)
            {
                if (rd.ItemId == hf.Value)
                {
                    delRD = rd;

                }
            }
            list.Remove(delRD);
            ReqDetailsView.DataSource = list;
            ReqDetailsView.DataBind();
            Session["RequisitionItems"] = list;
            //ModifyReqControl control = new ModifyReqControl();
            control.deleteReqDetails(i, hf.Value);
        }

        protected void ReqDetailsView_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }
    }
}